package fiszki.namespace;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class FiszkiSystemActivity extends Activity {
	private ImageButton check;
	private ImageButton next;
    private ImageButton back;
    private int setID;
    
    private FiszkiDBAdapter fiszkiDBAdapter;
    private Cursor fiszkiCursor;
    private List<FiszkaTasks> fiszki;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().format = android.graphics.PixelFormat.RGBA_8888;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //setContentView(R.layout.fiszki_system);
        
        initUiElements();
        initButtonsOnClickListeners();
        getAllFiszki();
        currentFiszka();
	}
	
	private void initUiElements() { 	
        //start = (ImageButton) findViewById(R.id.buttonStartLearning);
        back = (ImageButton) findViewById(R.id.buttonBack);
    	setID = getIntent().getExtras().getInt("setID");
    }
    
    private void initButtonsOnClickListeners() { 	 		
    	back.setOnClickListener(new View.OnClickListener() {
            
			@Override
			public void onClick(View v) {
            	finish();
            }
        });
	} 
    
    private void getAllFiszki() {
    	fiszkiDBAdapter = new FiszkiDBAdapter(getApplicationContext());
    	fiszkiDBAdapter.open();
        getAllFiszki();
        
        fiszki = new ArrayList<FiszkaTasks>();
        fiszkiCursor = getAllEntriesFromDB();
        
        if(fiszkiCursor != null && fiszkiCursor.moveToFirst()) {
            do {
                int id = fiszkiCursor.getInt(FiszkiDBAdapter.F_ID_COLUMN);
                int setID = fiszkiCursor.getInt(FiszkiDBAdapter.SET_COLUMN);
                String question = fiszkiCursor.getString(FiszkiDBAdapter.QUESTION_COLUMN);
                String answer = fiszkiCursor.getString(FiszkiDBAdapter.ANSWER_COLUMN);
                
                fiszki.add(new FiszkaTasks(id, setID, question, answer));
            } 
            while(fiszkiCursor.moveToNext());
        }
    }
     
    private Cursor getAllEntriesFromDB() {
        fiszkiCursor = fiszkiDBAdapter.getAllFiszki(setID);
        
        if(fiszkiCursor != null) {
            startManagingCursor(fiszkiCursor);
            fiszkiCursor.moveToFirst();
        }
        
        return fiszkiCursor;
    }
    
    private void currentFiszka() {
    	
    }
    
    private void nextFiszka() {
    	
    }
    
    private void updateFiszka() {
    	
    }
    
    @Override
    protected void onDestroy() {
        if(fiszkiDBAdapter != null)
        	fiszkiDBAdapter.close();
        
        super.onDestroy();
    }	
}
